package zust.javaee.service.Impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zust.javaee.dao.hibernate.BaseDao;
import zust.javaee.dto.announcement.AnnouncementDto;
import zust.javaee.entity.Tannouncement;
import zust.javaee.service.AnnouncementService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liy on 2017/1/9.
 */
@Service
@Transactional
public class AnnouncementServiceImpl  extends BaseServiceImpl<Tannouncement> implements AnnouncementService {

    @Autowired
    private BaseDao<Tannouncement> announcementDao;


    @Override
    public AnnouncementDto transToDto(Tannouncement tannouncement) {
        AnnouncementDto ann = new AnnouncementDto();
        BeanUtils.copyProperties(tannouncement, ann);
        return ann;
    }

    @Override
    public List<AnnouncementDto> transToDtos(List<Tannouncement> tannouncements) {
        List<AnnouncementDto> annDtos = new ArrayList<>();
        for (Tannouncement ann : tannouncements)
            annDtos.add(transToDto(ann));
        return annDtos;
    }

    @Override
    public Tannouncement getAnnouncemantById(Integer id) {
        String hql = "from Tannouncement a where a.id = " + id;
        return announcementDao.get(hql);
    }

    @Override
    public List<Tannouncement> getAnnouncementByCorporId(Integer corporId) {
        String hql = "from Tannouncement a where a.tcorporation.id = " + corporId;
        return announcementDao.find(hql);
    }

    @Override
    public List<Tannouncement> getAnnouncementByCorporId(Integer corporId, Integer num) {
        String hql = "from Tannouncement a where a.tcorporation.id = " + corporId;
        return announcementDao.find(hql, 1, num);
    }
}
